package  
{
	import org.flixel.plugin.photonstorm.FlxButtonPlus;
	import StoryGeneration.Location;
	import StoryGeneration.Story;
	import Tilemap;
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.FlxExtendedSprite;
	import Data.Color;
	import Data.Images;
	import ConversationClasses.*;
	import CharacterTraits.*;
	import StoryGeneration.Relationship;
	
	public class BaseGameState extends FlxState
	{
		public var inventory:Inventory;		// Inventory bag
		public var story:Story;				// Story of the game
		
		public static var invVisable:Boolean = false;
		
		// Text for rooms 
		private var roomText:FlxText;
		private var clueTextBackground:FlxSprite;
		
		//{ region Clue Spawn Points
		// Master Bedroom Clue Spawns
		private var mbClueSpawn1:FlxPoint = new FlxPoint(100, 110);
		private var mbClueSpawn2:FlxPoint = new FlxPoint(180, 30);
		private var mbClueSpawn3:FlxPoint = new FlxPoint(210, 250);
		
		// Billiard Room Clue Spawns
		private var brClueSpawn1:FlxPoint = new FlxPoint(400, 150);
		private var brClueSpawn2:FlxPoint = new FlxPoint(570, 220);
		private var brClueSpawn3:FlxPoint = new FlxPoint(440, 250);
		
		// Servant's Quarters Clue Spawns
		private var sqClueSpawn1:FlxPoint = new FlxPoint(760, 125);
		private var sqClueSpawn2:FlxPoint = new FlxPoint(710, 40);
		private var sqClueSpawn3:FlxPoint = new FlxPoint(800, 240);
		
		// Bathroom Clue Spawns
		private var bClueSpawn1:FlxPoint = new FlxPoint(200, 500);
		private var bClueSpawn2:FlxPoint = new FlxPoint(100, 430);
		private var bClueSpawn3:FlxPoint = new FlxPoint(250, 410);
		
		// Gallery Clue Spawns
		private var gClueSpawn1:FlxPoint = new FlxPoint(400, 420);
		private var gClueSpawn2:FlxPoint = new FlxPoint(530, 406);
		private var gClueSpawn3:FlxPoint = new FlxPoint(420, 570);
		
		// Dining Room Clue Spawns
		private var drClueSpawn1:FlxPoint = new FlxPoint(890, 440);
		private var drClueSpawn2:FlxPoint = new FlxPoint(680, 520);
		private var drClueSpawn3:FlxPoint = new FlxPoint(900, 520);
		
		// Lounge Clue Spawns
		private var lClueSpawn1:FlxPoint = new FlxPoint(30, 680);
		private var lClueSpawn2:FlxPoint = new FlxPoint(200, 690);
		private var lClueSpawn3:FlxPoint = new FlxPoint(100, 870);
		
		// Foyer Clue Spawns
		private var fClueSpawn1:FlxPoint = new FlxPoint(380, 900);
		private var fClueSpawn2:FlxPoint = new FlxPoint(580, 880);
		private var fClueSpawn3:FlxPoint = new FlxPoint(560, 740);
		
		// Kitchen Clue Spawns
		private var kClueSpawn1:FlxPoint = new FlxPoint(720, 680);
		private var kClueSpawn2:FlxPoint = new FlxPoint(900, 720);
		private var kClueSpawn3:FlxPoint = new FlxPoint(740, 900);
		//} endregion
		
		
		
		//{ region Clue items
		public var bloodyFootprint:Clue;
		public var book:Clue;
		public var candleStick:Clue;
		public var carpetTuft:Clue;
		public var cassette:Clue;
		public var cigaretteButt:Clue;
		public var cross:Clue;
		public var cyanide:Clue;
		public var knife:Clue;
		public var earrings:Clue;
		public var fabricScrap:Clue;
		public var fiberWire:Clue;
		public var glasses:Clue;
		public var hairStrand:Clue;
		public var handBag:Clue;
		public var key:Clue;
		public var lighter:Clue;
		public var lipstick:Clue;
		public var monocle:Clue;
		public var muddyFootprint:Clue;
		public var necklace:Clue;
		public var noose:Clue;
		public var note:Clue;
		public var note2:Clue;
		public var note3:Clue;
		public var note4:Clue;
		public var note5:Clue;
		public var gun:Clue;
		public var photos:Clue;
		public var pipe:Clue;
		public var ring:Clue;
		public var scarf:Clue;
		public var tornPaper:Clue;
		//} endregion
		
		
		//Environment Variables
		public var tilemap:Tilemap;
		public var levelSize:FlxPoint = new FlxPoint(960, 960);
		public var tileSize:FlxPoint = new FlxPoint(20 , 20);
		private var deadBodySpawn:FlxPoint;
		public var objectGroup:FlxGroup;
		public var deadBody:FlxSprite;
		
		public var playerSprite:Player;
		
		//Character Variables
		public var npcGroup:FlxGroup;
		public var spouce:Character;
		public var child:Character;
		public var lover:Character;
		public var buisnessPartner:Character;
		public var friend:Character;
		public var servant:Character;
		public var victim:Victim;
		private var targetRelation:String;
		
		//Room Variables
		public static var Foyer:Room;
		public static var Kitchen:Room;
		public static var Lounge:Room;
		public static var Gallery:Room;
		public static var Bathroom:Room;
		public static var DiningRoom:Room;
		public static var ServantsQuarters:Room;
		public static var BilliardRoom:Room;
		public static var MasterBedroom:Room;
		
		public static var FoyerArray:Array;
		public static var KitchenArray:Array;
		public static var LoungeArray:Array;
		public static var GalleryArray:Array;
		public static var BathroomArray:Array;
		public static var DiningRoomArray:Array;
		public static var ServantsQuartersArray:Array;
		public static var BilliardRoomArray:Array;
		public static var MasterBedroomArray:Array;
		
		//Interaction Variables
		public var conv:Conversation;
		public var interactionCheck:String;
		public var PerviousShortestInteractionDistance:Number;
		public var shortestInteractionDistance:Number;
		public var promptText:FlxText;
		
		// Text and buttons
		public var testText:FlxText; 
		public var motiveText:FlxText;
		public var murdererText:FlxText;
		public var locationText:FlxText;
		public var meansText:FlxText;
		
		public var storyGenerationButton:FlxButtonPlus;
		
		private var firstUpdate:Boolean = true;
		
		public function BaseGameState() 
		{			
			FlxG.mouse.show();
			
			//Level
			tilemap = new Tilemap(this, levelSize, tileSize);
			add(tilemap);
			
			deadBodySetup(); // Place the dead body
			
			objectGroup = new FlxGroup();
			decalSetup();
			
			// Rooms
			RoomSetup();
			
			// Clue Objects
			clueSetup();
			
			//Player Object
			playerSprite = new Player(455, 900, Images.DETECTIVE);
			playerSprite.setName(new Name("Andrew", "Vance")); //can get player to enter name or have static name
			Player.name.setNickName("Andy");
			add(playerSprite);
			
			characterSetup(); //Create Characters
			
			//Conversation System
			conv = new Conversation();
			add(conv);
			
			//Interaction Prompt Text
			promptText = new FlxText(0, 0, 100);
			promptText.setFormat(null, 16, Color.PurpleWOA, "center");
			add(promptText);
			
			// Inventory
			clueTextBackground = new FlxSprite(320);
			clueTextBackground.makeGraphic(320, 230, Color.Black);
			clueTextBackground.scrollFactor.x = 0;	clueTextBackground.scrollFactor.y = 0;
			clueTextBackground.visible = false;
			add(clueTextBackground);
			
			inventory = new Inventory();
			inventory.visible = false;
			add(inventory);
			
			//TODO: remove
			testText = new FlxText(30, 30, FlxG.width);
			testText.scrollFactor.x = 0;
			testText.scrollFactor.y = 0;
			add(testText);
			
			//Story
			StorySetup();
			
			storyGenerationButton = new FlxButtonPlus(30, 130, onStoryGeneration, null, "Generate Story");
			add(storyGenerationButton);
			
			MasterBedroomArray = new Array();
			MasterBedroomArray.push(new Location(207, 79, MasterBedroom));
			MasterBedroomArray.push(new Location(213, 238, MasterBedroom));
			MasterBedroomArray.push(new Location(73, 220, MasterBedroom));
			
			BilliardRoomArray = new Array();
			BilliardRoomArray.push(new Location(557, 275, BilliardRoom));
			BilliardRoomArray.push(new Location(401, 269, BilliardRoom));
			
			ServantsQuartersArray = new Array();
			ServantsQuartersArray.push(new Location(759, 257, ServantsQuarters));
			ServantsQuartersArray.push(new Location(886, 257, ServantsQuarters));
			ServantsQuartersArray.push(new Location(886, 107, ServantsQuarters));
			
			DiningRoomArray = new Array();
			DiningRoomArray.push(new Location(913, 419, DiningRoom));
			DiningRoomArray.push(new Location(898, 529, DiningRoom));
			DiningRoomArray.push(new Location(711, 423, DiningRoom));
			
			GalleryArray = new Array();
			GalleryArray.push(new Location(315, 443, Gallery));
			GalleryArray.push(new Location(418, 553, Gallery));
			GalleryArray.push(new Location(554, 448, Gallery));
			GalleryArray.push(new Location(557, 560, Gallery));
			
			BathroomArray = new Array();
			BathroomArray.push(new Location(247, 459, Bathroom));
			BathroomArray.push(new Location(250, 523, Bathroom));
			BathroomArray.push(new Location(106, 520, Bathroom));
			
			LoungeArray = new Array();
			LoungeArray.push(new Location(218, 750, Lounge));
			LoungeArray.push(new Location(208, 822, Lounge));
			LoungeArray.push(new Location(216, 684, Lounge));
			LoungeArray.push(new Location(113, 697, Lounge));
			
			FoyerArray = new Array();
			FoyerArray.push(new Location(375, 811, Foyer));
			FoyerArray.push(new Location(477, 737, Foyer));
			FoyerArray.push(new Location(575, 815, Foyer));
			
			KitchenArray = new Array();
			KitchenArray.push(new Location(743, 746, Kitchen));
			KitchenArray.push(new Location(836, 675, Kitchen));
			KitchenArray.push(new Location(928, 702, Kitchen));
		}
		
		private function deadBodySetup():void
		{
			// Random location for the dead body
			switch(Math.floor(Math.random() * 9))
			{
				case 0:
					deadBodySpawn = new FlxPoint(10, 180);
					break;
				case 1:
					deadBodySpawn = new FlxPoint(460, 100);
					break;
				case 2:
					deadBodySpawn = new FlxPoint(860, 240);
					break;
				case 3:
					deadBodySpawn = new FlxPoint(10, 510);
					break;
				case 4:
					deadBodySpawn = new FlxPoint(500, 540);
					break;
				case 5:
					deadBodySpawn = new FlxPoint(680, 410);
					break;
				case 6:
					deadBodySpawn = new FlxPoint(10, 800);
					break;
				case 7:
					deadBodySpawn = new FlxPoint(350, 720);
					break;
				case 8:
					deadBodySpawn = new FlxPoint(840, 880);
					break;
			}
			
			deadBody = new FlxSprite(deadBodySpawn.x, deadBodySpawn.y, Images.DEAD_BODY2);
			add(deadBody);
		}
		
		private function decalSetup():void
		{
			// Bottom layer
			add(new Decals(Images.DINING_RUG, 760, 440, 96, 58));
			add(new Decals(Images.RECT_RUG3, 420, 170, 120, 72));
			add(new Decals(Images.GREEN_ROUND_RUG, 435, 810, 70, 70));
			add(new Decals(Images.BATHROOM_RUG, 10, 440, 50, 35));
			
			// Master Bedroom
			objectGroup.add(new Decals(Images.KING_BED, 75, 7, 65, 80));
			objectGroup.add(new Decals(Images.LONG_COFFEE_TABLE, 87, 88, 40, 12));			
			objectGroup.add(new Decals(Images.DRESSER, 185, 295, 60, 15));
			objectGroup.add(new Decals(Images.DRESSER_LEFT, 255, 40, 15, 60));
			objectGroup.add(new Decals(Images.SMALL_ENDTABLE, 142, 10, 30, 19));
			objectGroup.add(new Decals(Images.SMALL_ENDTABLE, 42, 10, 30, 19));
			objectGroup.add(new Decals(Images.CLOCK, 25, 292, 22, 18));
			
			// Billiard Room
			objectGroup.add(new Decals(Images.POOL_TABLE, 440, 180, 80, 52));
			objectGroup.add(new Decals(Images.BAR, 420, 40, 120, 36));
			objectGroup.add(new Decals(Images.ARMCHAIR_RIGHT, 352, 90, 26, 30));
			objectGroup.add(new Decals(Images.ARMCHAIR_LEFT, 582, 90, 26, 30));
			objectGroup.add(new Decals(Images.ARMCHAIR_UP, 352, 280, 30, 26));
			objectGroup.add(new Decals(Images.ARMCHAIR_UP, 578, 280, 30, 26));
			
			// Servant's Quarters
			objectGroup.add(new Decals(Images.SINGLE_BED, 760, 10, 43, 80));
			objectGroup.add(new Decals(Images.SMALL_ENDTABLE, 806, 10, 30, 19));
			objectGroup.add(new Decals(Images.DRESSER_LEFT, 935, 180, 15, 60));
			objectGroup.add(new Decals(Images.PLANTER, 924, 282, 23, 24));
			
			// Bathroom
			objectGroup.add(new Decals(Images.SHOWER, 10, 390, 50, 45));
			objectGroup.add(new Decals(Images.TOILET, 65, 390, 30, 42));
			objectGroup.add(new Decals(Images.BATHROOM_SINK, 150, 390, 61, 30));
			
			
			// Gallery
			objectGroup.add(new Decals(Images.EASEL, 380, 580, 26, 26));
			objectGroup.add(new Decals(Images.SMALL_ENDTABLE, 380, 390, 30, 19));
			objectGroup.add(new Decals(Images.PICTURE_FRAME_DOWN, 510, 389, 50, 6));
			
			// Dining Room
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 770, 445, 20, 17));
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 789, 445, 20, 17));
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 808, 445, 20, 17));
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 828, 445, 20, 17));
			objectGroup.add(new Decals(Images.DINING_CHAIR_END, 760, 456, 18, 20));
			objectGroup.add(new Decals(Images.DINING_CHAIR_LEFT, 840, 456, 18, 20));
			objectGroup.add(new Decals(Images.DINING_TABLE, 770, 455, 80, 24));
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 770, 473, 20, 17));
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 789, 473, 20, 17));
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 808, 473, 20, 17));
			objectGroup.add(new Decals(Images.DINING_CHAIR_SIDE, 828, 473, 20, 17));
			
			// Lounge
			objectGroup.add(new Decals(Images.ENTERTAINMENT_CENTER, 85, 936, 80, 14));
			objectGroup.add(new Decals(Images.ARMCHAIR_RIGHT, 15, 870, 26, 30));
			objectGroup.add(new Decals(Images.OTTOMAN_SIDE, 47, 872, 19, 26));
			objectGroup.add(new Decals(Images.ARMCHAIR_LEFT, 212, 870, 26, 30));
			objectGroup.add(new Decals(Images.OTTOMAN_SIDE, 186, 872, 19, 26));
			objectGroup.add(new Decals(Images.COUCH, 94, 800, 60, 23));
			objectGroup.add(new Decals(Images.COFFE_TABLE, 104, 840, 40, 13));
			
			// Foyer
			objectGroup.add(new Decals(Images.PIANO, 446, 820, 40, 49));
			objectGroup.add(new Decals(Images.HALLWAY_TABLE_DOWN, 540, 688, 50, 18));
			objectGroup.add(new Decals(Images.PICTURE_FRAME_LEFT, 625, 720, 6, 50));
			objectGroup.add(new Decals(Images.PHONE_RIGHT, 330, 860, 13, 30));
			objectGroup.add(new Decals(Images.SMALL_ENDTABLE, 340, 690, 30, 19));
			
			// Kitchen
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER_CORNER, 920, 920, 30, 30));
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER_SIDE, 925, 891, 25, 30));
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER_SIDE, 925, 861, 25, 30));
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER_SIDE, 925, 831, 25, 30));
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER_SIDE, 925, 801, 25, 30));
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER, 891, 925, 30, 25));
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER, 866, 925, 30, 25));
			objectGroup.add(new Decals(Images.KITCHEN_COUNTER, 841, 925, 30, 25));
			objectGroup.add(new Decals(Images.FRIDGE, 922, 771, 28, 30));
			objectGroup.add(new Decals(Images.STOVE, 813, 920, 28, 30));
			objectGroup.add(new Decals(Images.OVAL_COFFEE_TABLE, 800, 740, 70, 37));
			objectGroup.add(new Decals(Images.KITCHEN_CHAIR_DOWN, 810, 727, 20, 13));
			objectGroup.add(new Decals(Images.KITCHEN_CHAIR_DOWN, 840, 727, 20, 13));
			objectGroup.add(new Decals(Images.KITCHEN_CHAIR, 810, 777, 20, 13));
			objectGroup.add(new Decals(Images.KITCHEN_CHAIR, 840, 777, 20, 13));
			
			// Hallways
			objectGroup.add(new Decals(Images.SCONCE_DOWN, 40, 319, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_UP, 40, 372, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_DOWN, 900, 319, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_UP, 900, 372, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_DOWN, 40, 579, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_UP, 40, 632, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_DOWN, 900, 579, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_UP, 900, 632, 20, 9));
			objectGroup.add(new Decals(Images.SCONCE_RIGHT, 279, 40, 9, 20));
			objectGroup.add(new Decals(Images.SCONCE_LEFT, 332, 40, 9, 20));
			objectGroup.add(new Decals(Images.SCONCE_RIGHT, 619, 40, 9, 20));
			objectGroup.add(new Decals(Images.SCONCE_LEFT, 672, 40, 9, 20));
			objectGroup.add(new Decals(Images.SCONCE_RIGHT, 259, 900, 9, 20));
			objectGroup.add(new Decals(Images.SCONCE_LEFT, 312, 900, 9, 20));
			objectGroup.add(new Decals(Images.SCONCE_RIGHT, 639, 900, 9, 20));
			objectGroup.add(new Decals(Images.SCONCE_LEFT, 692, 900, 9, 20));
			objectGroup.add(new Decals(Images.PICTURE_FRAME_RIGHT, 279, 250, 6, 50));
			objectGroup.add(new Decals(Images.PICTURE_FRAME_RIGHT, 619, 210, 6, 50));
			objectGroup.add(new Decals(Images.PICTURE_FRAME_UP, 200, 375, 50, 6));
			objectGroup.add(new Decals(Images.PICTURE_FRAME_UP, 740, 375, 50 , 6));
			objectGroup.add(new Decals(Images.PICTURE_FRAME_DOWN, 696, 579, 50, 6));
			objectGroup.add(new Decals(Images.HALLWAY_TABLE_UP, 520, 364, 50, 18));
			objectGroup.add(new Decals(Images.PHONE, 230, 580, 30, 13));
			add(objectGroup);
			
			// Top Layer
			add(new Decals(Images.LAMP, 50, 13, 14, 13));
			add(new Decals(Images.LAMP, 150, 13, 14, 13));
			add(new Decals(Images.LAMP, 812, 12, 14, 13));
			add(new Decals(Images.STATUE, 387, 393, 16, 12));
			add(new Decals(Images.STATUE, 346, 692, 16, 12));
			add(new Decals(Images.SINK, 936, 860, 10, 24));
		}
		
		private function clueSetup():void
		{
			bloodyFootprint = new Clue(Images.BLOODY_FOOTPRINT, 0, 0, 11, 30);
			book = new Clue(Images.BOOK, 0, 0, 30, 18);
			candleStick = new Clue(Images.CANDLESTICK, 200, 500, 11, 30);
			carpetTuft = new Clue(Images.CARPET_TUFT, 0, 0, 30, 21);
			cassette = new Clue(Images.CARPET_TUFT, 0, 0, 30, 8);
			cigaretteButt = new Clue(Images.CIGARETTE_BUTT, 0, 0, 31, 8);
			cross = new Clue(Images.CROSS, 0, 0, 16, 30);
			cyanide = new Clue(Images.CYANIDE, 400, 150, 11, 30);
			knife = new Clue(Images.KNIFE, 760, 125, 11, 30);
			earrings = new Clue(Images.EARRINGS, 0, 0, 27, 30);
			fabricScrap = new Clue(Images.FABRIC_SCRAP, 0, 0, 23, 30);
			fiberWire = new Clue(Images.FIBERWIRE, 560, 740, 30, 32);
			glasses = new Clue(Images.GLASSES, 0, 0, 30, 12);
			hairStrand = new Clue(Images.HAIR_STRAND, 0, 0, 8, 30);
			handBag = new Clue(Images.HANDBAG, 0, 0, 30, 27);
			key = new Clue(Images.KEY, 0, 0, 30, 11);
			lighter = new Clue(Images.LIGHTER, 0, 0, 18, 30);
			lipstick = new Clue(Images.LIPSTICK, 0, 0, 7, 30);
			monocle = new Clue(Images.MONOCLE, 0, 0, 30, 29);
			muddyFootprint = new Clue(Images.MUDDY_FOOTPRINT, 0, 0, 11, 30);
			necklace = new Clue(Images.NECKLACE, 0, 0, 30, 27);
			noose = new Clue(Images.NOOOSE, 890, 440, 15, 30);
			note = new Clue(Images.NOTE, mbClueSpawn1.x, mbClueSpawn1.y, 28, 30);
			note2 = new Clue(Images.NOTE_2, 0, 0, 28, 30);
			note3 = new Clue(Images.NOTE_3, 0, 0, 14, 30);
			note4 = new Clue(Images.NOTE_4, 0, 0, 14, 30);
			note5 = new Clue(Images.NOTE_5, 0, 0, 30, 29);
			gun = new Clue(Images.GUN, 820, 900, 30, 15);
			photos = new Clue(Images.PHOTOS, 0, 0, 18, 30);
			pipe = new Clue(Images.PIPE, 0, 0, 30, 28);
			ring = new Clue(Images.RING, 0, 0, 19, 30);
			scarf = new Clue(Images.SCARF, 0, 0, 12, 35);
			tornPaper = new Clue(Images.TORN_PAPER, 0, 0, 27, 30);
			
			// Add random clues at random points
			add(candleStick);
			add(cyanide);
			add(knife);
			add(fiberWire);
			add(noose);
			add(note);
			add(gun);
		}
		
		private function characterSetup():void
		{
			npcGroup = new FlxGroup();
			add(npcGroup);
			
			spouce = new Character(880, 50, new Name("Edith", "Fortunato"), 40, Gender.FEMALE, HairColor.BLACK, EyeColor.GREEN, new Personality(), Relationship.WIFE, tilemap.wallTilemap, Images.SPOUSE);
			spouce.personality.SetPersonality(.5, .5, .3, .5, .7);
			npcGroup.add(spouce);
			
			child = new Character(880, 100, new Name("Madeline", "Fortunato"), 40, Gender.FEMALE, HairColor.BLONDE, EyeColor.HAZEL, new Personality(), Relationship.DAUGHTER, tilemap.wallTilemap, Images.DAUGHTER);
			child.name.setNickName("Maddy");
			child.personality.SetPersonality(.7, .5, .5, .7, .5);
			npcGroup.add(child);
			
			lover = new Character(450, 150, new Name("Anne", "Faulkner"), 40, Gender.FEMALE, HairColor.BROWN, EyeColor.BROWN, new Personality(), Relationship.LOVER, tilemap.wallTilemap, Images.MISTRESS);
			lover.personality.SetPersonality(.9, .3, .7, .9, .3);
			npcGroup.add(lover);
			
			buisnessPartner = new Character(800, 500, new Name("Lawrence", "Wargrave", "John"), 40, Gender.MALE, HairColor.BROWN, EyeColor.BROWN, new Personality(), Relationship.BIZ_PARTNER, tilemap.wallTilemap, Images.BUSINESSMAN);
			buisnessPartner.personality.SetPersonality(.3, .9, .5, .3, .3);
			npcGroup.add(buisnessPartner);
			
			friend = new Character(800, 800, new Name("Johnathan", "Arbuthnot"), 40, Gender.MALE, HairColor.DARK_BROWN, EyeColor.BLUE, new Personality(), Relationship.FRIEND, tilemap.wallTilemap, Images.FRIEND);
			friend.name.setNickName("John");
			friend.personality.SetPersonality(.5, .3, .7, .7, .7);
			npcGroup.add(friend);
			
			servant = new Character(450, 800, new Name("Delbert", "Grady"), 40, Gender.MALE, HairColor.GREY, EyeColor.BROWN, new Personality(), Relationship.SERVANT, tilemap.wallTilemap, Images.BUTLER);
			servant.personality.SetPersonality(.3, .7, .3, .5, .5);
			npcGroup.add(servant);
			
			victim = new Victim(450, 450);
			Victim.name = new Name("Harold", "Fortunato");
			Victim.name.setNickName("Harry");
		}
		
		private function StorySetup():void
		{
			story = new Story();
			
			motiveText = new FlxText(FlxG.width / 2  - 50, 20, FlxG.width);
			murdererText = new FlxText(FlxG.width / 2  - 50, 40, FlxG.width);
			locationText = new FlxText(FlxG.width / 2  - 50, 60, FlxG.width);
			meansText = new FlxText(FlxG.width / 2  - 50, 80, FlxG.width);
			refreshText();
			motiveText.scrollFactor.x = 0;
			motiveText.scrollFactor.y = 0;
			murdererText.scrollFactor.x = 0;
			murdererText.scrollFactor.y = 0;
			locationText.scrollFactor.x = 0;
			locationText.scrollFactor.y = 0;
			meansText.scrollFactor.x = 0;
			meansText.scrollFactor.y = 0;
			add(motiveText);
			add(murdererText);
			add(locationText);
			add(meansText);
		}
		
		private function RoomSetup():void
		{
			//creates FlxRect objects that overlap the room and contain a sting with the name of that room for character reference
			Foyer = new Room(325, 685, 310, 280, "foyer", 65, "Foyer");
			Kitchen = new Room(705, 645, 250, 310, "kitchen", 45, "Kitchen");
			Lounge = new Room(5, 645, 250, 310, "lounge", 125, "Lounge");
			Gallery = new Room(365, 385, 230, 230, "gallery", 75, "Gallery");
			Bathroom = new Room(5, 385, 290, 190, "bathroom", 55, "Bathroom", Color.BlackWOA);
			DiningRoom = new Room(665, 385, 290, 190, "dining room", 25, "Dining Room");
			ServantsQuarters = new Room(685, 5, 270, 310, "servant's quarters", 145, "Servant's Quarters");
			BilliardRoom = new Room(345, 5, 270, 310, "billiard room", 85, "Billiard Room");
			MasterBedroom = new Room(5, 5, 270, 310, "master bedroom", 145, "Master Bedroom");
		}
		
		private function roomTextSetup():void
		{
			add(Foyer.displayText);
			add(Kitchen.displayText);
			add(Lounge.displayText);
			add(Gallery.displayText);
			add(Bathroom.displayText);
			add(DiningRoom.displayText);
			add(ServantsQuarters.displayText);
			add(BilliardRoom.displayText);
			add(MasterBedroom.displayText);
		}
		
		override public function update():void 
		{
			super.update();
			
			FlxG.camera.setBounds(0, 0, 960, 960, true);
			FlxG.camera.follow(playerSprite, FlxCamera.STYLE_TOPDOWN);
			
			if (firstUpdate)
			{
				firstUpdate = false;
				FlxG.camera.focusOn(new FlxPoint(playerSprite.x + playerSprite.width / 2, playerSprite.y + playerSprite.height / 2));
				
				//add room text
				roomTextSetup();
			}
			
			FlxG.collide(playerSprite, tilemap.wallGroup);
			
			FlxG.collide(playerSprite, objectGroup);
			
			//FlxG.collide(playerSprite, npcGroup);
			
			playerInteraction();
			
			checkInventory();
			
			ConversationCheck();
			
			//TODO: remove
			testText.text = "X: " + FlxG.mouse.x.toFixed() + " Y: " + FlxG.mouse.y.toFixed();
			testText.scrollFactor.x = 0;
			testText.scrollFactor.y = 0;
		}
		
		private function ConversationCheck():void
		{
			if (Conversation.inConversation)
			{
				if (FlxG.keys.justPressed("Q"))
				{
					conv.EndConversation(GetConversationTarget());
				}
			}
		}
		
		private function GetConversationTarget():Character
		{
			if (Conversation.talkingTo == Relationship.WIFE)
			{
				return spouce; 
			}
			else if (Conversation.talkingTo == Relationship.DAUGHTER)
			{
				return child; 
			}
			else if (Conversation.talkingTo == Relationship.SERVANT)
			{
				return servant; 
			}
			else if (Conversation.talkingTo == Relationship.BIZ_PARTNER)
			{
				return buisnessPartner; 
			}
			else if (Conversation.talkingTo == Relationship.FRIEND)
			{
				return friend; 
			}
			else if (Conversation.talkingTo == Relationship.LOVER)
			{
				return lover; 
			}
			
			return null;
		}
		
		private function playerInteraction():void
		{
			resetInteractionVars();
			
			//rooms
			checkRoom(Foyer);
			checkRoom(Kitchen);
			checkRoom(Lounge);
			checkRoom(Gallery);
			checkRoom(Bathroom);
			checkRoom(DiningRoom);
			checkRoom(ServantsQuarters);
			checkRoom(BilliardRoom);
			checkRoom(MasterBedroom);
			
			targetRelation = null;
			
			//conversations
			checkConversation(spouce);
			checkConversation(child);
			checkConversation(lover);
			checkConversation(buisnessPartner);
			checkConversation(friend);
			checkConversation(servant);
			
			if (promptText.visible == true && FlxG.keys.justPressed("SPACE") && !Conversation.inConversation && targetRelation != null)
			{
				conv.BeginConversation(getTargetCharacter(targetRelation), playerSprite);
			}
			
			//clues
			checkClue(note);
			checkClue(fiberWire);
			checkClue(noose);
			checkClue(gun);
			checkClue(candleStick);
			checkClue(cyanide);
			checkClue(knife);
			
			//nothing in range
			if (interactionCheck == InteractionCheck.NONE)
			{
				promptText.visible = false;
			}
			else if (Conversation.inConversation)
			{
				promptText.visible = false;
				
				if (getCurrentRoom() != null)
				{
					getCurrentRoom().displayText.visible = false;
				}
			}
		}
		
		private function resetInteractionVars():void
		{
			PerviousShortestInteractionDistance = shortestInteractionDistance;
			shortestInteractionDistance = 100;
			interactionCheck = InteractionCheck.NONE;
		}
		
		private function checkRoom(room:Room):Boolean
		{
			var temp:Boolean;
			
			if ((playerSprite.x <= room.x + room.width) && (playerSprite.x + playerSprite.width >= room.x) && 
				(playerSprite.y <= room.y + room.height) && (playerSprite.y + playerSprite.height >= room.y))
			{
				if (inventory.visible == false)
				{
					room.Toggle(true);
				}
				else
				{
					room.Toggle(false);
				}
				temp = true;
			}
			else
			{
				room.Toggle(false);
				temp = false;
			}
			
			return temp;
		}
		
		private function getCurrentRoom():Room
		{
			if (checkRoom(Foyer))
			{
				return Foyer;
			}
			if (checkRoom(Kitchen))
			{
				return Kitchen;
			}
			if (checkRoom(Lounge))
			{
				return Lounge;
			}
			if (checkRoom(Gallery))
			{
				return Gallery;
			}
			if (checkRoom(Bathroom))
			{
				return Bathroom;
			}
			if (checkRoom(DiningRoom))
			{
				return DiningRoom;
			}
			if (checkRoom(ServantsQuarters))
			{
				return ServantsQuarters;
			}
			if (checkRoom(BilliardRoom))
			{
				return BilliardRoom;
			}
			if (checkRoom(MasterBedroom))
			{
				return MasterBedroom;
			}
			
			return null;
		}
		
		private function checkConversation(character:Character):void
		{
			var temp:Number = FlxU.getDistance(new FlxPoint(playerSprite.x + playerSprite.width / 2, playerSprite.y + playerSprite.height / 2), 
												new FlxPoint(character.x + character.width / 2, character.y + character.height / 2));
			
			if (temp < Character.TALK_RANGE && temp < shortestInteractionDistance)
			{
				shortestInteractionDistance = temp;
				interactionCheck = InteractionCheck.TALK;
				promptText.text = InteractionCheck.TALK;
				promptText.x = character.x - promptText.width / 2 + character.width / 2;
				promptText.y = character.y - 20;
				promptText.visible = true;
				targetRelation = character.relationship;
			}
			
			//if (promptText.visible == true && FlxG.keys.justPressed("SPACE") && !Conversation.inConversation && temp == PerviousShortestInteractionDistance)
			//if (promptText.visible == true && FlxG.keys.justPressed("SPACE") && !Conversation.inConversation)
			//{
				//conv.BeginConversation(getTargetCharacter(targetRelation), playerSprite);
			//}
		}
		
		private function getTargetCharacter(relation:String):Character
		{
			if (relation == Relationship.WIFE)
			{
				return spouce;
			}
			else if (relation == Relationship.DAUGHTER)
			{
				return child;
			}
			else if (relation == Relationship.SERVANT)
			{
				return servant;
			}
			else if (relation == Relationship.BIZ_PARTNER)
			{
				return buisnessPartner;
			}
			else if (relation == Relationship.FRIEND)
			{
				return friend;
			}
			else if (relation == Relationship.LOVER)
			{
				return lover;
			}
			
			return null;
		}
		
		private function checkClue(clue:Clue):void
		{
			var temp:Number = FlxU.getDistance(new FlxPoint(playerSprite.x + playerSprite.width / 2, playerSprite.y + playerSprite.height / 2), 
												new FlxPoint(clue.x + clue.width / 2, clue.y + clue.height / 2));
			
			if (temp < Clue.PICK_UP_RANGE && temp < shortestInteractionDistance)
			{
				shortestInteractionDistance = temp;
				interactionCheck = InteractionCheck.PICK_UP;
				promptText.text = InteractionCheck.PICK_UP;
				promptText.x = clue.x - promptText.width / 2 + clue.width / 2;
				promptText.y = clue.y - 20;
				promptText.visible = true;
			}
			
			//Add clues to inventory
			if (promptText.visible == true && FlxG.keys.justPressed("SPACE") && !Conversation.inConversation && temp == PerviousShortestInteractionDistance)
			{
				inventory.addClue(clue);	// Add the clue
				remove(clue);				// Remove the clue from the world
			}
		}
		
		// The "I" key opens/closes the inventory screen
		private function checkInventory():void
		{
			if (FlxG.keys.justPressed("I") && !inventory.visible)
			{
				inventory.visible = true;	// Display the inventory bag
				invVisable = true;
				clueTextBackground.visible = true;
				//FlxG.timeScale = 0;
			}
			else if (FlxG.keys.justPressed("I") && inventory.visible)
			{
				inventory.visible = false;	// Hide the inventory bag
				invVisable = false;
				clueTextBackground.visible = false;
				//FlxG.timeScale = 1;
			}
			
			if (invVisable)
			{
				inventory.changeSelection();
			}
		}
		
		private function onStoryGeneration():void
		{
			if (story == null)
			{
				story = new Story();
			}
			else
			{
				story.generateStory();
				refreshText();
			}
		}
		
		private function refreshText():void
		{
			motiveText.text = "Motive " + story.motive;
			murdererText.text = "Murderer: " +  story.murderer;
			locationText.text = "Location: " + story.location;
			meansText.text = "Means: " + story.means;
		}
	}
}